﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.TcReloadProduct.CUiTcReloadProductDialog
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.Utilities;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Engineering.Ui.WPF;
using UiInternalControls.WPFControls;


namespace DnProcessSimulateCommands.TcCommands.TcReloadProduct;

public class CUiTcReloadProductDialog : TxWindow, IComponentConnector
{
  private static bool m_reloadAllParts = true;
  private static bool m_loadNewParts = false;
  private bool m_updateIsRunning;
  private bool m_documentWasUnloaded;
  internal CUiTcReloadProductDialog ReloadProductDialog;
  internal StackPanel Stackpanel1;
  internal System.Windows.Controls.CheckBox cbRelaodAllParts;
  internal TxInfoButton infoReloadAllParts;
  internal System.Windows.Controls.CheckBox cbLoadNewParts;
  internal TxInfoButton infoLoadNewParts;
  internal TxObjectGridControl gridSelectedOps;
  internal StackPanel Stackpanel3;
  internal System.Windows.Controls.Button btnUpdate;
  internal System.Windows.Controls.Button btnClose;
  internal System.Windows.Controls.Primitives.StatusBar statusBar;
  internal StatusBarItem stBarItemText;
  private bool _contentLoaded;

  public CUiTcReloadProductDialog() => this.InitializeComponent();

  public bool RealodAllParts
  {
    get => CUiTcReloadProductDialog.m_reloadAllParts;
    set => CUiTcReloadProductDialog.m_reloadAllParts = value;
  }

  public bool LoadNewParts
  {
    get => CUiTcReloadProductDialog.m_loadNewParts;
    set => CUiTcReloadProductDialog.m_loadNewParts = value;
  }

  private void InitOperatinsGridColData()
  {
    // ISSUE: unable to decompile the method.
  }

  internal void UpdateStatusText(string stText, bool doRefresh = false)
  {
    this.stBarItemText.Content = (object) stText;
    if (!doRefresh)
      return;
    this.stBarItemText.Refresh();
  }

  private void UpdateStatusTextDueToGridStateChange(bool isChecked)
  {
    if (this.m_documentWasUnloaded)
      return;
    if (isChecked && this.gridSelectedOps.Count == 0)
      this.UpdateStatusText(TcReloadProductStringTable.RELOAD_PRODUCT_STATUS_SELECT_OPERATIONS);
    else
      this.UpdateStatusText(string.Empty);
  }

  private void UpdateEnableBtnState()
  {
    bool? isChecked1 = this.cbLoadNewParts.IsChecked;
    bool flag1 = true;
    if (isChecked1.GetValueOrDefault() == flag1 & isChecked1.HasValue)
    {
      this.btnUpdate.IsEnabled = ((Collection<ITxObject>) this.gridSelectedOps.Objects).Count > 0;
    }
    else
    {
      System.Windows.Controls.Button btnUpdate = this.btnUpdate;
      bool? isChecked2 = this.cbRelaodAllParts.IsChecked;
      bool flag2 = true;
      int num = isChecked2.GetValueOrDefault() == flag2 & isChecked2.HasValue ? 1 : 0;
      btnUpdate.IsEnabled = num != 0;
    }
  }

  private void DisableControls()
  {
    this.cbRelaodAllParts.IsEnabled = false;
    this.cbLoadNewParts.IsEnabled = false;
    ((UIElement) this.gridSelectedOps).IsEnabled = false;
    this.btnUpdate.IsEnabled = false;
    this.btnClose.IsEnabled = false;
  }

  private void EnableControls()
  {
    this.cbRelaodAllParts.IsEnabled = true;
    this.cbLoadNewParts.IsEnabled = true;
    TxObjectGridControl gridSelectedOps1 = this.gridSelectedOps;
    bool? isChecked1 = this.cbLoadNewParts.IsChecked;
    bool flag1 = true;
    int num1 = isChecked1.GetValueOrDefault() == flag1 & isChecked1.HasValue ? 1 : 0;
    ((UIElement) gridSelectedOps1).IsEnabled = num1 != 0;
    TxObjectGridControl gridSelectedOps2 = this.gridSelectedOps;
    bool? isChecked2 = this.cbLoadNewParts.IsChecked;
    bool flag2 = true;
    int num2 = isChecked2.GetValueOrDefault() == flag2 & isChecked2.HasValue ? 1 : 0;
    gridSelectedOps2.ListenToPick = num2 != 0;
    this.UpdateEnableBtnState();
    this.btnClose.IsEnabled = true;
  }

  private void gridSelectedOps_ObjectInserted(
    object sender,
    TxObjGridCtrl_ObjectInsertedEventArgs args)
  {
    this.UpdateEnableBtnState();
    bool? isChecked = this.cbLoadNewParts.IsChecked;
    bool flag = true;
    this.UpdateStatusTextDueToGridStateChange(isChecked.GetValueOrDefault() == flag & isChecked.HasValue);
  }

  private void gridSelectedOps_RowDeleted(object sender, TxObjGridCtrl_RowDeletedEventArgs args)
  {
    this.UpdateEnableBtnState();
    bool? isChecked = this.cbLoadNewParts.IsChecked;
    bool flag = true;
    this.UpdateStatusTextDueToGridStateChange(isChecked.GetValueOrDefault() == flag & isChecked.HasValue);
  }

  private void gridSelectedOps_Picked(object sender, TxObjGridCtrl_PickedEventArgs args)
  {
    if (sender == this || !((UIElement) this.gridSelectedOps).IsEnabled)
      return;
    this.gridSelectedOps.AppendObject(args.Obj);
    this.gridSelectedOps.Focus();
  }

  private void ReloadProductDialog_Initialized(object sender, EventArgs e)
  {
    this.InitOperatinsGridColData();
    this.cbRelaodAllParts.IsChecked = new bool?(this.RealodAllParts);
    this.cbLoadNewParts.IsChecked = new bool?(this.LoadNewParts);
    this.HandleCbLoadNewPartsStateChanged(this.LoadNewParts);
    this.UpdateEnableBtnState();
  }

  private void HandleCbLoadNewPartsStateChanged(bool isChecked)
  {
    ((UIElement) this.gridSelectedOps).IsEnabled = isChecked;
    this.gridSelectedOps.ListenToPick = isChecked;
    this.UpdateStatusTextDueToGridStateChange(isChecked);
  }

  private void cbLoadNewParts_Checked(object sender, RoutedEventArgs e)
  {
    this.HandleCbLoadNewPartsStateChanged(true);
    this.gridSelectedOps.Focus();
    this.UpdateEnableBtnState();
  }

  private void cbLoadNewParts_Unchecked(object sender, RoutedEventArgs e)
  {
    this.HandleCbLoadNewPartsStateChanged(false);
    this.UpdateEnableBtnState();
  }

  private void cbRelaodAllParts_Checked(object sender, RoutedEventArgs e)
  {
    this.UpdateEnableBtnState();
  }

  private void cbRelaodAllParts_Unchecked(object sender, RoutedEventArgs e)
  {
    this.UpdateEnableBtnState();
  }

  private List<TxProcessModelId> GetProcessModelIds(TxObjectList objects)
  {
    List<TxProcessModelId> processModelIds = new List<TxProcessModelId>();
    foreach (ITxObject itxObject in (Collection<ITxObject>) objects)
    {
      if (itxObject is ITxProcessModelObject processModelObject)
        processModelIds.Add(processModelObject.ProcessModelId);
    }
    return processModelIds;
  }

  private void ReloadOperationsIntoGrid(List<TxProcessModelId> objectsIds)
  {
    TxObjectList txObjectList = new TxObjectList();
    foreach (TxProcessModelId objectsId in objectsIds)
    {
      ITxObject byProcessModelId = TxApplication.ActiveDocument.GetObjectByProcessModelId(objectsId);
      if (byProcessModelId != null)
        ((Collection<ITxObject>) txObjectList).Add(byProcessModelId);
    }
    this.gridSelectedOps.Objects = txObjectList;
  }

  private void Update()
  {
    if (!TcCommandsUtility.PromptSaveStudyAndProceed(TcReloadProductStringTable.RELOAD_PRODUCT_SAVE_TEXT, TcReloadProductStringTable.RELOAD_PRODUCT_CMD_NAME))
      return;
    this.UpdateStatusText(TcReloadProductStringTable.RELOAD_PRODUCT_STATUS_UPDATING, true);
    List<TxProcessModelId> processModelIds = this.GetProcessModelIds(this.gridSelectedOps.Objects);
    Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
    TxErrorStack.Clear();
    try
    {
      this.DisableControls();
      this.m_updateIsRunning = true;
      bool? isChecked = this.cbRelaodAllParts.IsChecked;
      bool flag1 = isChecked.Value;
      isChecked = this.cbLoadNewParts.IsChecked;
      bool flag2 = isChecked.Value;
      ITxTcGlobalServicesProvider servicesProvider = TxApplication.ActiveDocument.PlatformGlobalServicesProvider as ITxTcGlobalServicesProvider;
      if (flag1 && !flag2)
      {
        servicesProvider.ReloadProduct();
      }
      else
      {
        if (!flag2 || this.gridSelectedOps.Count <= 0)
          return;
        servicesProvider.UpdateProduct(this.gridSelectedOps.Objects, new TxTcUpdateProductParams()
        {
          RunReloadProduct = flag1
        });
      }
    }
    catch (TxException ex)
    {
      int num = (int) TxMessageBox.Show(TcReloadProductStringTable.RELOAD_PRODUCT_FAILURE, TcReloadProductStringTable.RELOAD_PRODUCT_CMD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    finally
    {
      this.m_updateIsRunning = false;
      Mouse.OverrideCursor = (System.Windows.Input.Cursor) null;
      this.UpdateStatusText(TcReloadProductStringTable.RELOAD_PRODUCT_STATUS_UPDATE_FINISHED);
      this.EnableControls();
      if (this.m_documentWasUnloaded)
      {
        this.ReloadOperationsIntoGrid(processModelIds);
        this.m_documentWasUnloaded = false;
      }
      this.gridSelectedOps.LoseFocus();
      TxErrorStackDlg.DisplayErrors();
    }
  }

  private void ActiveDocument_Unloading(object sender, TxDocument_UnloadingEventArgs args)
  {
    this.m_documentWasUnloaded = this.m_updateIsRunning;
    if (this.m_updateIsRunning)
      return;
    ((Window) this).Close();
  }

  private void ReloadProductDialog_Closing(object sender, CancelEventArgs e)
  {
    this.UnregisterFromEvents();
    this.RealodAllParts = this.cbRelaodAllParts.IsChecked.Value;
    this.LoadNewParts = this.cbLoadNewParts.IsChecked.Value;
    ((Collection<ITxObject>) this.gridSelectedOps.Objects).Clear();
    this.gridSelectedOps.UnInitialize();
  }

  private void btnUpdate_Click(object sender, RoutedEventArgs e) => this.Update();

  private void btnClose_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void RegisterToEvents()
  {
    this.gridSelectedOps.ObjectInserted += new TxObjGridCtrl_ObjectInsertedEventHandler(this.gridSelectedOps_ObjectInserted);
    this.gridSelectedOps.RowDeleted += new TxObjGridCtrl_RowDeletedEventHandler(this.gridSelectedOps_RowDeleted);
    this.gridSelectedOps.Picked += new TxObjGridCtrl_PickedEventHandler(this.gridSelectedOps_Picked);
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
  }

  private void UnregisterFromEvents()
  {
    this.gridSelectedOps.ObjectInserted -= new TxObjGridCtrl_ObjectInsertedEventHandler(this.gridSelectedOps_ObjectInserted);
    this.gridSelectedOps.RowDeleted -= new TxObjGridCtrl_RowDeletedEventHandler(this.gridSelectedOps_RowDeleted);
    this.gridSelectedOps.Picked -= new TxObjGridCtrl_PickedEventHandler(this.gridSelectedOps_Picked);
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.ActiveDocument_Unloading);
  }

  private void ReloadProductDialog_Loaded(object sender, RoutedEventArgs e)
  {
    Size availableSize = new Size(double.PositiveInfinity, this.cbLoadNewParts.ActualHeight);
    this.cbRelaodAllParts.Measure(availableSize);
    this.cbLoadNewParts.Measure(availableSize);
    Size desiredSize = this.cbRelaodAllParts.DesiredSize;
    double width1 = desiredSize.Width;
    desiredSize = this.cbLoadNewParts.DesiredSize;
    double width2 = desiredSize.Width;
    double num1 = Math.Max(width1, width2);
    double width3 = ((FrameworkElement) this.infoLoadNewParts).Width;
    Thickness margin = this.Stackpanel1.Margin;
    double left1 = margin.Left;
    margin = this.Stackpanel1.Margin;
    double right1 = margin.Right;
    double num2 = left1 + right1;
    margin = this.cbLoadNewParts.Margin;
    double left2 = margin.Left;
    double num3 = num2 + left2;
    margin = ((FrameworkElement) this.infoLoadNewParts).Margin;
    double right2 = margin.Right;
    double num4 = num3 + right2;
    double num5 = num1 + width3 + num4;
    if (((FrameworkElement) this).ActualWidth < num5)
    {
      // ISSUE: explicit non-virtual call
      __nonvirtual (((FrameworkElement) this).Width) = num5;
    }
    if (((FrameworkElement) this).MinWidth >= num5)
      return;
    ((FrameworkElement) this).MinWidth = num5;
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/tccommands/tcreloadproduct/cuitcreloadproductdialog.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.ReloadProductDialog = (CUiTcReloadProductDialog) target;
        ((FrameworkElement) this.ReloadProductDialog).Initialized += new EventHandler(this.ReloadProductDialog_Initialized);
        ((Window) this.ReloadProductDialog).Closing += new CancelEventHandler(this.ReloadProductDialog_Closing);
        ((FrameworkElement) this.ReloadProductDialog).Loaded += new RoutedEventHandler(this.ReloadProductDialog_Loaded);
        break;
      case 2:
        this.Stackpanel1 = (StackPanel) target;
        break;
      case 3:
        this.cbRelaodAllParts = (System.Windows.Controls.CheckBox) target;
        this.cbRelaodAllParts.Checked += new RoutedEventHandler(this.cbRelaodAllParts_Checked);
        this.cbRelaodAllParts.Unchecked += new RoutedEventHandler(this.cbRelaodAllParts_Unchecked);
        break;
      case 4:
        this.infoReloadAllParts = (TxInfoButton) target;
        break;
      case 5:
        this.cbLoadNewParts = (System.Windows.Controls.CheckBox) target;
        this.cbLoadNewParts.Checked += new RoutedEventHandler(this.cbLoadNewParts_Checked);
        this.cbLoadNewParts.Unchecked += new RoutedEventHandler(this.cbLoadNewParts_Unchecked);
        break;
      case 6:
        this.infoLoadNewParts = (TxInfoButton) target;
        break;
      case 7:
        this.gridSelectedOps = (TxObjectGridControl) target;
        break;
      case 8:
        this.Stackpanel3 = (StackPanel) target;
        break;
      case 9:
        this.btnUpdate = (System.Windows.Controls.Button) target;
        this.btnUpdate.Click += new RoutedEventHandler(this.btnUpdate_Click);
        break;
      case 10:
        this.btnClose = (System.Windows.Controls.Button) target;
        this.btnClose.Click += new RoutedEventHandler(this.btnClose_Click);
        break;
      case 11:
        this.statusBar = (System.Windows.Controls.Primitives.StatusBar) target;
        break;
      case 12:
        this.stBarItemText = (StatusBarItem) target;
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
